package com.nf.health.app.customview;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.nf.health.app.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 折线图的横线和YLable
 * 
 * @author fei
 * 
 */
public class BrokenLineBackground extends View {

	// private List<Float> data;
	private int mWidth, mHeight;
	private int marginLeft, marginBottom, marginAxis;
	private int yCount = 4;
	private int yInterval;
	private float tb;
	private Paint paint_date, paint_brokenLine, paint_dottedline,
			paint_brokenline_big, framPanint;
	private List<String> list = new ArrayList<String>();
	private List<String> y_array = new ArrayList<String>();

	private int fineLineColor = 0x5faaaaaa; // 灰色
	private int blueLineColor = 0xff00ffff; // 蓝色
	private int orangeLineColor = 0xffd56f2b; // 橙色
	private int marginTop;
	private int goalPosition;
	private float y1;

	public BrokenLineBackground(Context context, List<String> y_array,
			int goalPosition) {
		super(context);
		marginLeft = (int) getResources().getDimension(
				R.dimen.broken_line_xmargin);
		marginBottom = (int) getResources().getDimension(
				R.dimen.broken_line_ymargin);
		marginAxis = (int) getResources().getDimension(
				R.dimen.broken_line_margin_yaxis);
		marginTop = (int) getResources().getDimension(
				R.dimen.broken_line_margin_top);

		this.y_array = y_array;
		yCount = y_array.size() - 1;
		this.goalPosition = goalPosition;
		getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						mWidth = getWidth();
						mHeight = getHeight();
						yInterval = (mHeight - marginBottom) / yCount;
						getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
					}
				});

		init();
	}

	public BrokenLineBackground(Context context, List<String> y_array) {
		super(context);

		marginTop = (int) getResources().getDimension(
				R.dimen.broken_line_margin_top);
		marginLeft = (int) getResources().getDimension(
				R.dimen.broken_line_xmargin);
		marginBottom = (int) getResources().getDimension(
				R.dimen.broken_line_ymargin);
		marginAxis = (int) getResources().getDimension(
				R.dimen.broken_line_margin_yaxis);

		this.y_array = y_array;
		getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						mWidth = getWidth();
						mHeight = getHeight();
						yInterval = (mHeight - marginBottom) / yCount;
						getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
					}
				});

		init();
	}

	public void init() {
		// if (null == data || data.size() == 0)
		// return;
		// this.data = data;
		Resources res = getResources();
		tb = res.getDimension(R.dimen.historyscore_tb);

		paint_date = new Paint();
		paint_date.setAntiAlias(true);
		paint_date.setStrokeWidth(tb * 0.1f);
		paint_date.setTextSize(tb * 1.0f);
		paint_date.setTextAlign(Paint.Align.RIGHT);
		paint_date.setColor(Color.BLACK);

		paint_brokenLine = new Paint();
		paint_brokenLine.setStrokeWidth(tb * 0.1f);
		paint_brokenLine.setColor(blueLineColor);
		paint_brokenLine.setAntiAlias(true);

		paint_dottedline = new Paint();
		paint_dottedline.setStyle(Paint.Style.STROKE);
		paint_dottedline.setColor(fineLineColor);

		paint_brokenline_big = new Paint();
		paint_brokenline_big.setStrokeWidth(tb * 0.2f);
		paint_brokenline_big.setColor(fineLineColor);
		paint_brokenline_big.setAntiAlias(true);

		framPanint = new Paint();
		framPanint.setAntiAlias(true);
		framPanint.setStrokeWidth(2f);

		// setLayoutParams(new LayoutParams(
		// (int) (this.data.size() * interval_left_right),
		// LayoutParams.MATCH_PARENT));
	}

	protected void onDraw(Canvas c) {
		// if (null == data || data.size() == 0)
		// return;
		drawHorizontalLine(c);
		drawYLabel(c);
	}

	/**
	 * 绘制横线
	 * 
	 * @param c
	 */
	public void drawHorizontalLine(Canvas c) {
		// 绘制X轴,向下偏移10个像素
		c.drawLine(marginLeft, yInterval * yCount + marginTop, getWidth(),
				yInterval * yCount + marginTop, paint_dottedline);
		// 绘制虚线
		if (goalPosition == 0) {
			for (int i = 0; i < yCount; i++) {
				paint_dottedline.setColor(orangeLineColor);
				Path path = new Path();
				path.moveTo(marginLeft, yInterval * i + marginTop);
				path.lineTo(getWidth(), yInterval * i + marginTop);
				PathEffect effects = new DashPathEffect(new float[] {
						tb * 0.3f, tb * 0.3f, tb * 0.3f, tb * 0.3f }, tb * 0.1f);
				paint_dottedline.setPathEffect(effects);
				c.drawPath(path, paint_dottedline);
				// c.drawLine(xMargin, yInterval * i, getWidth(), yInterval * i,
				// paint_dottedline);
			}
		} else {
			for (int i = 0; i < yCount; i++) {
				if (goalPosition == i) {
					paint_brokenLine.setColor(orangeLineColor);
					float base = getHeight() - marginBottom;
					y1 = base
							- (base * (Float.parseFloat(y_array.get(i))) / Float
									.parseFloat(y_array.get(2))) + marginTop;
					c.drawLine(marginLeft, y1, getWidth(), y1, paint_brokenLine);
				}
			}
		}
	}

	public void drawYLabel(Canvas c) {
		FontMetrics fontMetrics = paint_date.getFontMetrics();
		float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
		float offY = fontTotalHeight / 2 - fontMetrics.bottom + marginTop;
		if(goalPosition == 0){
			for (int i = 0; i < y_array.size(); i++) {
				c.drawText(y_array.get(i), marginLeft - marginAxis, yInterval
						* (y_array.size() - 1 - i) + offY, paint_date);
			}
		}else{
			for (int i = 0; i < y_array.size(); i++) {
				if (goalPosition == i) {
					c.drawText("目标："+y_array.get(i), marginLeft/2, y1, paint_date);
				}
			}
		}
	}
}
